﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OUtl.Setting;

namespace TalkServer.common
{
    public partial class frmBase : Form
    {
        public frmBase()
        {
            InitializeComponent();
        }

        private void frmBase_Load(object sender, EventArgs e)
        {

        }

        #region >>Info<<
        protected bool isStart = false;
        protected bool isSend = false; 
        protected string AppName = null;
        protected string UserName { get; set; }
        protected string Password { get; set; }

        protected DateTime StartTime = DateTime.Now;

        protected string SportKey = "";
        #endregion

        #region >>AppClient<<
        protected MSGAPP.Client clientApp = null;

        protected void appStart()
        {
            if (isStart)
            {
                isStart = false;
                if (clientApp != null)
                    clientApp.Stop();
                clientApp = null;
            }
            else
            {
                isStart = true;
                StartTime = DateTime.Now;
                clientApp = new MSGAPP.Client(AppName, AppName, Settings.ServerIP, MSGGolbalInfo.Settings.DicDefPort["APP"]);
                clientApp.ClientStateEvent += new MSGGolbalInfo.ClientStateEventArgs(clientApp_ClientStateEvent);
                clientApp.DataReceivedEvent += new EventHandler(clientApp_DataReceivedEvent);
                clientApp.CommandReceivedEvent += new EventHandler(clientApp_CommandReceivedEvent);
                clientApp.ResultsReceivedEvent += new EventHandler(clientApp_ResultsReceivedEvent);
                clientApp.CommonReceivedEvent += new EventHandler(clientApp_CommonReceivedEvent);
                clientApp.SettingEvent += new EventHandler(clientApp_SettingEvent);
                clientApp.Start();
                setAppInfo();
            }
            Start(!isStart);
        }

        protected virtual void clientApp_ResultsReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientApp_CommonReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientApp_ClientStateEvent(object sender, TCPGolbalInfo.FlagEnum flag)
        {
        }

        protected virtual void clientApp_CommandReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientApp_DataReceivedEvent(object sender, EventArgs e)
        {
        }

        protected virtual void clientApp_SettingEvent(object sender, EventArgs e)
        {

        }

        protected virtual void setAppInfo()
        {
            
        }
        #endregion

        #region >>Start<<
        protected virtual void Start(bool isStop)
        {
        }
        #endregion

        #region >>ServerIP<<

        /// <summary>
        /// 设定服务器IP
        /// </summary>
        protected void setSetverIP()
        {
            frmServerIP frm = new frmServerIP(Settings.ServerIP);
            DialogResult dr = frm.ShowDialog(this);
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                if (frm.ServerIP != Settings.ServerIP)
                {
                    Settings.ServerIP = frm.ServerIP;
                    ServerIPset();
                }
            }
            if (frm != null && !frm.IsDisposed)
                frm.Dispose();
            frm = null;
        }

        /// <summary>
        /// 服务器IP被设定
        /// </summary>
        protected virtual void ServerIPset()
        {
        }
        #endregion

        #region >>Exit<<
        protected void ExitAPP()
        {
            Application.Exit();
            System.Threading.Thread.Sleep(1000);
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }
        #endregion

        #region >>Debug_WriteLine<<
        public static bool isDebug { get; set; }
        protected static void Debug_WriteLine(string msg)
        {
            if (OUtl.OutputBox.isSay && isDebug)
                Console.WriteLine(msg);
        }

        protected static void Debug_WriteLine(string format, params object[] arg)
        {
            if (OUtl.OutputBox.isSay && isDebug)
                Console.WriteLine(format, arg);
        }
        #endregion
    }
}
